package com.component;

import java.util.List;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;

import com.base.attachment.entity.Attachment;
import com.base.attachment.service.AttachmentException;
import com.base.attachment.service.impl.AttachmentService;
import com.base.commons.SpringBeanHolder;

@ManagedBean(name = "component_listAttachmentBean", scope = ManagedBeanScope.REQUEST)
public class ListAttachmentBean {
	private AttachmentService attachmentServcie = SpringBeanHolder
			.getBean("attachmentSrv");
	@Bind(id = "attachmentsList", attribute = "binding")
	private UIDataGrid attachmentsList_binding;
	@SaveState
	@Accessible
	private String beanClass = FacesContext.getCurrentInstance()
			.getExternalContext().getRequestParameterMap().get("beanClass");
	@SaveState
	@Accessible
	private String beanId = FacesContext.getCurrentInstance()
			.getExternalContext().getRequestParameterMap().get("beanId");
	@SaveState
	@Accessible
	private String attachmentType = FacesContext.getCurrentInstance()
			.getExternalContext().getRequestParameterMap()
			.get("attachmentType");

	@DataModel(id = "attachmentsList")
	public List<Attachment> getAll() {
		try {
			return attachmentServcie.getAttachmentsByBeanAndType(beanClass,
					beanId, attachmentType);
		} catch (AttachmentException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Action(id = "delFile")
	public void delFile() {
		Object[] objs = attachmentsList_binding.getSelectedValues();
		try {
			for (Object att : objs) {
				attachmentServcie.removeAttachment((Attachment) att);
			}
		} catch (AttachmentException e) {
			e.printStackTrace();
		}
		attachmentsList_binding.reload();
	}
}
